package com.djsearch.data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.apache.http.NameValuePair;

import android.util.Log;
/**
 * 请求服务器类
 * @author chenyang2
 *
 */
public class HttpRequestManager {
	
	private static HttpRequestManager httpRequestManager = null;
	/**
	 * 单例，返回该类实例
	 * @return httpRequestManager
	 */
	public static HttpRequestManager getInstance() {
		if (httpRequestManager == null) {
			httpRequestManager = new HttpRequestManager();
		}
		return httpRequestManager;
	}
	
	
	/**
	 * post请求服务端数据
	 * @param uriAPI 请求路径
	 * @param TimeOut
	 * @return 服务器返回的json格式字符串
	 */
	public String getStringFromServer(String uriAPI,int timeOut) {
		/**
		 * 设置超时和连接时间
		 */
		HttpClient client = new DefaultHttpClient();
		//Represents a collection of HTTP protocol and framework parameters
		HttpParams httpParams = null;
		httpParams = client.getParams();
		//set timeout
		HttpConnectionParams.setConnectionTimeout(httpParams, timeOut);
		HttpConnectionParams.setSoTimeout(httpParams, timeOut);
		/**
		 * 请求数据
		 */
		 //建立HTTP Post连线  
	    HttpPost httpRequest =new HttpPost(uriAPI);  
	    List <NameValuePair> paramsList = new ArrayList<NameValuePair>();  
	    /*paramsList.add(new BasicNameValuePair("userName","张三"));  
	    paramsList.add(new BasicNameValuePair("pwd", "123456"));*/
	    try{  
	    	 //发出HTTP request  
		    httpRequest.setEntity((HttpEntity) new UrlEncodedFormEntity(paramsList,HTTP.UTF_8));  
		     //取得HTTP response  
		    HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
		     //若状态码为200
		    if (httpResponse.getStatusLine().getStatusCode() == 200) {
		    	//取出回应字串  
			    String strResult=EntityUtils.toString(httpResponse.getEntity());  
			    return strResult;
	        } else {
	        	//错误
	        	return "";
	        }
	    }catch(ClientProtocolException e){  
	    	e.printStackTrace();
	    } catch (UnsupportedEncodingException e) {  
	    	e.printStackTrace(); 
	    } catch (IOException e) {  
	    	e.printStackTrace();  
	    }  
	    
	    return "";
	    
	   }  
	
	
	
	}
		
		
		  
	       
	

